/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.panomedic.gui;


import com.panomedic.Constants;
import com.panomedic.core.Photo;
import com.panomedic.core.Photos;
import com.panomedic.log4j.Logger;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.prefs.Preferences;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Yare
 */
public class AppFrame extends JFrame implements com.panomedic.gui.Frame,
        ListSelectionListener
{
    static Logger logger = new Logger();

    private Menu appMenu;
    
    private Preferences prefs;
    
    protected JPanel northPanel;
    protected JPanel centerPanel;
    protected StatusBar southPanel;
    protected JSplitPane splitPane;
    protected int splitWidth;
    protected JScrollPane scrollPane;
    protected PhotoPanel photoPanel = null;
    protected ThumbList list = null;
    
    private JProgressBar progressBar;
    private ToolBar toolBar;
    
    public AppFrame() {
    
        super();
        prefs = Preferences.userRoot().node(Constants.defNodePath);
    }

    public void Create() {

        
        setTitle(Constants.APPLICATION_NAME);
        
        BorderLayout layout = new BorderLayout();
        setLayout(layout);        
        
        //westPanel
//        westPanel = new JPanel();
//        westPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
//        westPanel.setPreferredSize(new Dimension(50, 60));
//        westPanel.setBackground(Color.BLUE);
//        westPanel.add(new JLabel("This is thumbs panel"));
//        add(westPanel, BorderLayout.WEST);
        
        //centerPanel
//        centerPanel = new JPanel();
//        centerPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
//        add(centerPanel, BorderLayout.CENTER);        
        
        //scrolPane
       
       
        DefaultListModel listModel = //Kernel.getInstance().getPhotos();
                                       Photos.getInstance();
       
        if (listModel != null) {
            list = new ThumbList((Photos)listModel,200);        //const
            
        }
        
//        MouseListener mouseListener = new MouseAdapter() {
//
//            public void mouseClicked(MouseEvent e) {
//                if ( e.getButton() == MouseEvent.BUTTON2 ){
//                    
//                }
//            }
//        }
       
//        list.addMouseListener(mouseListener);
        list.addListSelectionListener(this);
        list.setPreferredSize(new Dimension(150,100));
        
        
        scrollPane = new JScrollPane(list, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        
        //photoPanel
        photoPanel = new PhotoPanel();
        
        
        //splitPane (centerPanel)
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scrollPane,photoPanel);
        splitPane.setDividerSize(6);
        splitWidth = prefs.getInt(Constants.SPLIT_WIDTH, WIDTH);
        splitPane.setDividerLocation(200);          //const
        splitPane.setOneTouchExpandable(false);
        add(splitPane,BorderLayout.CENTER);
        
        
        //southPanel
        southPanel = new StatusBar();
        add(southPanel, BorderLayout.SOUTH);
        
        // northPanel
        toolBar = new ToolBar();
        
        
//        progressBar = new JProgressBar();
//        progressBar.setVisible(false);
                      
//        northPanel = new JPanel();
//        northPanel.add(toolBar);
        add(toolBar,BorderLayout.PAGE_START);
        
        //menu
        appMenu = new Menu();
        setJMenuBar(appMenu);
        
        setBackground(Color.BLACK);
        setSize(prefs.getInt(Constants.FRAME_WIDTH, 800), prefs.getInt(Constants.FRAME_HEIGHT, 600));
        setLocation(prefs.getInt(Constants.FRAME_X, 100), prefs.getInt(Constants.FRAME_Y, 100));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }


    public void initProgressBar(int max) {
        progressBar.setMinimum(0);
        progressBar.setMaximum(max);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressBar.setVisible(true);
        
    }
    public void setProgress(int value) {
        if (value == progressBar.getMaximum()) {
            setCursor(null);
            progressBar.setVisible(false);
        } else {
            progressBar.setValue(value);
            Rectangle progressRect = progressBar.getBounds();
            progressRect.x = 0;
            progressRect.y = 0;
            progressBar.paintImmediately(progressRect);
            progressBar.invalidate();
        }
    }
    
    public int getSplitWidth() {
        return splitWidth;
    }
   
    
    public Menu getMenu() {
        return appMenu;
    }

    public void valueChanged(ListSelectionEvent e) {
        //logger.info(LogCategories.PROCESSING, "Selected:"+list.getSelectedIndex());
        Photo photo = (Photo)list.getSelectedValue();
        selectPhoto(photo);
    }

    public void selectPhoto(Photo photo) {
        setPhotoView(photo);
    }
    
    public void setPhotoView(Photo photo){
        photoPanel.setPhoto(photo);
        
    }
    public void setPhotoView(BufferedImage image){
        photoPanel.setImage(image);
    }
    
    public void setPhotoView(){
        photoPanel.setImage(null);
    }

    public ThumbList getList() {
        return list;
    }
    
    public void setStatus(String s) {
        southPanel.setMessage(s);
    }

    public PhotoPanel getPhotoPanel() {
        return photoPanel;
    }

    public ToolBar getToolBar() {
        return toolBar;
    }
    
    
}
